/**
* 基础菜单 商品管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>pdfToText</el-breadcrumb-item>
    </el-breadcrumb>

    <br>
    <br>
    <br>

    <el-button type="primary" plain @click="test1_">单文件上传</el-button>
    <el-button type="success" plain @click="test2_">文件夹上传</el-button>
    <div v-show="test1">
    <el-row>
      <el-col :span="8" :offset="8">
        <!-- 用户上传 -->
        <el-input v-model="filename" placeholder="上传后自动显示文件名" readonly></el-input>
      </el-col>
    </el-row>
    <br>
    <el-row>
      <el-col :span="8" :offset="8">
        <!-- 用户上传 -->
        <el-select v-model="filetag" placeholder="请选择分类">
          <el-option
            v-for="(item, index) in typeOptions"
            :key="item.id"
            :label="item.tag"
            :value="item.id"/>
        </el-select>
      </el-col>
    </el-row>

    <br>
    <el-row>
      <el-col :span="8" :offset="8">
        <el-upload
          class="upload-demo"
          ref="upload"
          action="http://192.168.2.88:10001/pdfToText/pdfToText"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :http-request="httpRequest"
          :on-change="handleChange"
          :file-list="fileList"
          :auto-upload="false">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <div slot="tip" class="el-upload__tip">请上传PDF文件</div>
        </el-upload>
      </el-col>
    </el-row>

    <br>
    <el-row>
      <el-col :span="8" :offset="10">
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">单文件上传并提取文字</el-button>
      </el-col>
    </el-row>

    <br>
<!--    <hr>-->
    </div>
    <br>
    <br>
    <div v-show="test2">
    <el-row>
      <el-col :span="8" :offset="8">
        <!-- 用户上传 -->
        <el-upload
          class="upload-demo"
          ref="uploadDir"
          action="/"
          :auto-upload="false"
          :file-list="fileListDir"
          :http-request="httpRequest"
          multiple>
          <el-button slot="trigger" size="small" type="primary">选取文件夹</el-button>
          <div slot="tip" class="el-upload__tip">请上传PDF文件</div>
        </el-upload>
      </el-col>
    </el-row>
    <br>
    <el-row>
      <el-col :span="8" :offset="8">
        <!-- 用户上传 -->
        <el-select v-model="filetag" placeholder="请选择分类">
          <el-option
            v-for="(item, index) in typeOptions"
            :key="item.id"
            :label="item.tag"
            :value="item.id"/>
        </el-select>
      </el-col>
    </el-row>
    <br>
    <el-row>
      <el-col :span="8" :offset="10">
        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitpiliangUpload">批量上传并提取文字</el-button>
      </el-col>
    </el-row>
    </div>

    <br>

    <template>
<!--      <el-button type="text" @click="open">点击打开 Message Box</el-button>-->
    </template>
  </div>
</template>

<script>
import { deptList, deptSave, deptDelete } from '../../api/userMG'
import Pagination from '../../components/Pagination'
import axios from "axios";
export default {
  data() {
    return {
      test1:true,
      test2:false,
      filetag : "",
      filetag1 : "",
      fileworkname:"",
      resData:"",
      fileList: [],
      filename:"",
      // old_data
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        deptId: '',
        deptName: '',
        deptNo: '',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      formInline: {
        page: 1,
        limit: 10,
        varLable: '',
        varName: '',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      },

      typeOptions: [],
      fileListDir: []
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
    this.initOptions()
  },

  mounted() {
    this.$refs.uploadDir.$refs['upload-inner'].$refs.input.setAttribute('webkitdirectory', '')
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    test1_(){
      this.test1 = true
      this.test2 = false
    },
    test2_(){
      this.test1 = false
      this.test2 = true
    },
    open() {
      this.$alert(this.resData, '提取文本', {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: `action: ${ action }`
          });
        }
      });
    },
    // param是自带参数。 this.$refs.upload.submit() 会自动调用 httpRequest方法.在里面取得file
    httpRequest(param) {
      console.log(param)
      let fileObj = param.file // 相当于input里取得的files
      let fd = new FormData()// FormData 对象
      fd.append('file', fileObj)// 文件对象
      fd.append('name',this.filename)
      if(this.filetag == undefined || this.filetag == ""){

        fd.append('tag',new Date().getTime())
      }else{

        fd.append('tag',this.filetag)
      }

      let url = this.$global.javaUrl + "/pdfToText/pdfToText"
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      axios.post(url, fd, config).then(res=>{

        if(res.code===0){
          // this.submitForm();//提交表单
        }
        this.resData = res.data.data;
        // this.open();
        this.$message({
          type: 'info',
          message: `上传成功`
        });

        // window.location.reload();

        console.log("log")
        console.log(res.data.data)
      })
    },
    submitUpload() {
      this.$refs.upload.submit();
      // this.fileListDir = []
      this.fileList=[]
      this.filename = ""
      this.filetag = ""
    },
    submitpiliangUpload() {
      this.$refs.uploadDir.submit();
      this.fileListDir = []
      this.filename = ""
      this.filetag = ""
      // if (this.fileworkname != "") {
      //   let url = this.$global.javaUrl + "pdfToText/piliangUnStruct"
      //   let config = {
      //     headers: {
      //       'Content-Type': 'multipart/form-data',
      //       'user': localStorage.getItem('userId')
      //     }
      //   }
      //   if(this.filetag1 == undefined || this.filetag1 == ""){
      //     this.filetag1 = new Date().getTime()
      //   }
      //   axios.get(url, {
      //     headers:
      //       {
      //         'user': localStorage.getItem('userId')
      //       },
      //     params:
      //       {
      //         path:this.fileworkname,
      //
      //         tag:this.filetag1
      //       }
      //   }).then(res => {
      //     if (res.status == 200) {
      //       console.log("返回信息")
      //       console.log(res)
      //       var list_data = res.data.data.fields
      //       this.filedoptions = []
      //       for (var i = 0; i < list_data.length; i++) {
      //         this.filedoptions[i] = {label: list_data[i], value: list_data[i]}
      //       }
      //     } else {
      //
      //     }
      //   })
      // }else{ //if
      //
      //       this.$message.error('请输入路径')
      // }

    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },

    //old_data
    // 获取公司列表
    getdata(parameter) {
      this.loading = true
      // 模拟数据开始
      let res = {
        code: 0,
        msg: null,
        count: 5,
        data: [
          {
            addUser: null,
            editUser: null,
            addTime: 1521062371000,
            editTime: 1526700200000,
            deptId: 2,
            deptName: 'XX分公司',
            deptNo: '1',
            parentId: 1
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1521063247000,
            editTime: 1526652291000,
            deptId: 3,
            deptName: '上海测试',
            deptNo: '02',
            parentId: 1
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526349555000,
            editTime: 1526349565000,
            deptId: 12,
            deptName: '1',
            deptNo: '11',
            parentId: 1
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526373178000,
            editTime: 1526373178000,
            deptId: 13,
            deptName: '5',
            deptNo: '5',
            parentId: 1
          },
          {
            addUser: null,
            editUser: null,
            addTime: 1526453107000,
            editTime: 1526453107000,
            deptId: 17,
            deptName: 'v',
            deptNo: 'v',
            parentId: 1
          }
        ]
      }
      this.loading = false
      this.listData = res.data
      this.pageparm.currentPage = this.formInline.page
      this.pageparm.pageSize = this.formInline.limit
      this.pageparm.total = res.count
      // 模拟数据结束

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      // deptList(parameter)
      //   .then(res => {
      //     this.loading = false
      //     if (res.success == false) {
      //       this.$message({
      //         type: 'info',
      //         message: res.msg
      //       })
      //     } else {
      //       this.listData = res.data
      //       // 分页赋值
      //       this.pageparm.currentPage = this.formInline.page
      //       this.pageparm.pageSize = this.formInline.limit
      //       this.pageparm.total = res.count
      //     }
      //   })
      //   .catch(err => {
      //     this.loading = false
      //     this.$message.error('菜单加载失败，请稍后再试！')
      //   })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.deptId = row.deptId
        this.editForm.deptName = row.deptName
        this.editForm.deptNo = row.deptNo
      } else {
        this.title = '添加'
        this.editForm.deptId = ''
        this.editForm.deptName = ''
        this.editForm.deptNo = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          deptSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '公司保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('公司保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除公司
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deptDelete(row.deptId)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '公司已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('公司删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    },

    initOptions() {
      axios.get(this.$global.javaUrl + '/modelFiletag/list?pageSize=100&pageNum=1', {
        headers:
          {
            'user': localStorage.getItem('userId')
          }
      }).then(res => {
        this.typeOptions = res.data.data.rows;
      })
    },
    handleChange(param) {
      this.filename = param.raw.name;
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>


